package com.songlanyun.modules.coupon.service.impl.shopcoupon;

import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.coupon.model.dto.SelectPlatformCouponDTO;
import com.songlanyun.modules.coupon.model.dto.SelectShopCouponDTO;
import com.songlanyun.modules.coupon.model.dto.SelectShopCouponGoodsDTO;
import com.songlanyun.modules.coupon.model.vo.CouponEntityVo;
import com.songlanyun.modules.coupon.model.vo.CouponReceiveRecordVo;
import com.songlanyun.modules.coupon.service.CouponReceiveRecordService;
import com.songlanyun.modules.coupon.service.ISelectPlatformCouponService;
import com.songlanyun.modules.coupon.service.ISelectShopCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SelectPlatformCouponServiceImpl implements ISelectPlatformCouponService {
    @Autowired
    private CouponReceiveRecordService couponReceiveRecordService;

    @Override
    public List<CouponReceiveRecordVo> selectPlatfromCoupons(Long userId, SelectPlatformCouponDTO dto) {
        List<CouponReceiveRecordVo> couponEntityVos = couponReceiveRecordService.loadMyPlatCoupons(userId);
        List<CouponReceiveRecordVo> result = new ArrayList<>();
        for (CouponReceiveRecordVo couponEntityVo : couponEntityVos) {
            if (handle(dto, couponEntityVo)) {
                result.add(couponEntityVo);
            }
        }
        return result;
    }


    private boolean handle(SelectPlatformCouponDTO dto, CouponReceiveRecordVo couponEntityVo) {

        switch (dto.getMarketingType())
        {
            case 1:
                if(!couponEntityVo.isAdaptSecKill())
                {
                    return false;
                }
                break;
            case 2:
                if(!couponEntityVo.isAdaptGroupBuy())
                {
                    return false;
                }
                break;
            default:
                break;
        }
        if (couponEntityVo.getUniversal()) {
            if (generalCouponHandle(couponEntityVo, dto)) {
                return true;
            }
        } else {
            if (notGeneralCouponHandle(couponEntityVo, dto)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 非通用券算法
     *
     * @param couponEntityVo
     * @param dto
     * @return
     */
    private boolean notGeneralCouponHandle(CouponReceiveRecordVo couponEntityVo, SelectPlatformCouponDTO dto) {
        if (!couponEntityVo.getHasThreshold()) {
            return noHasThresholdNotGeneral(couponEntityVo, dto);
        } else {
            return hasThresholdNotGeneral(couponEntityVo, dto);
        }
    }

    private boolean noHasThresholdNotGeneral(CouponReceiveRecordVo couponEntityVo, SelectPlatformCouponDTO dto) {
        BigDecimal totalAmount = getTotalAmount(couponEntityVo,dto);
        if(totalAmount.compareTo(BigDecimal.ZERO)==0)
        {
            return false;
        }
        return true;
    }

    private boolean hasThresholdNotGeneral(CouponReceiveRecordVo couponEntityVo, SelectPlatformCouponDTO dto) {
        BigDecimal totalAmount = getTotalAmount(couponEntityVo,dto);
        if(totalAmount.compareTo(BigDecimal.ZERO)==0)
        {
            return false;
        }
        if (totalAmount.compareTo(couponEntityVo.getThreshold()) < 0) {
            return false;
        }
        return true;
    }


    private BigDecimal getTotalAmount(CouponReceiveRecordVo couponEntityVo, SelectPlatformCouponDTO dto) {
        Map<Long, SelectShopCouponDTO> shopOrderGroups = dto.getShopOrderList().stream().collect(Collectors.toMap(SelectShopCouponDTO::getShopId, vo -> vo));
        Set<Long> shopIds = shopOrderGroups.keySet();
        List<Long> adaptIds = couponEntityVo.loadAdaptShopIds();
        if (couponEntityVo.getAdaptOrNo()) {
            shopIds.retainAll(adaptIds);

        } else {
            shopIds.removeAll(adaptIds);
        }

        BigDecimal totalAmountDecShopCoupon = BigDecimal.ZERO;
        for (Long shopId : shopIds) {
            SelectShopCouponDTO submitShopOrderVo = shopOrderGroups.get(shopId);
            totalAmountDecShopCoupon = MathUtils.add(submitShopOrderVo.getTotalAmountDecShopCoupon(), totalAmountDecShopCoupon);
        }
        return totalAmountDecShopCoupon;
    }




    /**
     * 通用券算法
     *
     * @param couponEntityVo
     * @param dto
     * @return
     */
    private boolean generalCouponHandle(CouponReceiveRecordVo couponEntityVo, SelectPlatformCouponDTO dto) {
        if (!couponEntityVo.getHasThreshold()) {
            return true;
        } else {
            return hasThresholdGeneral(couponEntityVo, dto);
        }
    }

    private boolean hasThresholdGeneral(CouponReceiveRecordVo couponEntityVo, SelectPlatformCouponDTO dto) {
        return dto.getTotalAmountDecShopCoupon().compareTo(couponEntityVo.getThreshold()) >= 0;
    }


}
